package org.particlethink.mapper.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.particlethink.exception.TokenException;
import org.particlethink.utils.PathUtil;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DictRequestHelper {
    private final static String DictUrl = "/system/dict/type/find";
    private final static ObjectMapper objectMapper = new ObjectMapper();


    public static List<String> queryDictionaries(String _url, String dictionaryName, String token) throws URISyntaxException, IOException {
        List<String> dictionaries;

        try (HttpResponse response = HttpUtil.createPost(_url + DictUrl).body(objectMapper.writeValueAsString(new Request(dictionaryName))).header("Authorization", "Bearer " + token).execute()) {
            Response resp = objectMapper.readValue(response.bodyBytes(), Response.class);
            if (resp.code == 401) {
                throw new TokenException();
            }
            if (resp.getData() == null) {
                return new ArrayList<>();
            }

            dictionaries = new ArrayList<String>(resp.getData().getTypes());
        }

        return dictionaries;
    }

    public static class Data {
        private List<String> types;

        public List<String> getTypes() {
            return types;
        }

        public void setTypes(List<String> types) {
            this.types = types;
        }
    }

    public static class Response {
        private String msg;
        private Integer code;
        private Data data;

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public Data getData() {
            return data;
        }

        public void setData(Data data) {
            this.data = data;
        }
    }

    public static class Request {
        private String dictName;

        public Request(String dictName) {
            this.dictName = dictName;
        }

        public Request() {
        }

        public String getDictName() {
            return dictName;
        }

        public void setDictName(String dictName) {
            this.dictName = dictName;
        }
    }

    public static DicTypes Dict(String baseUrl, String token, String dictName) throws IOException {

        Map<String, Object> params = new HashMap<>();
        params.put("dictType", dictName);

        try (HttpResponse response = HttpUtil.createPost(baseUrl + "/system/dict/type/get").body(objectMapper.writeValueAsString(params), "application/json").header("Authorization", "Bearer " + token).execute()) {
            SccResponse resp = objectMapper.readValue(response.bodyBytes(), SccResponse.class);

            if (resp.getCode() == 401) {
                throw new TokenException();
            }
            return resp.getData();
        }
    }


    public static class SccResponse {
        private String msg;
        private Integer code;
        private DicTypes data;

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public DicTypes getData() {
            return data;
        }

        public void setData(DicTypes data) {
            this.data = data;
        }
    }


    public static class DicTypes {
        private List<String> types;

        public List<String> getTypes() {
            return types;
        }

        public void setTypes(List<String> types) {
            this.types = types;
        }
    }

}
